const app = getApp();
const api = require('../../../ws_micro_shop/utils/api.js');
const { throttle } = require('../../common/utils/util');

Page({
  data: {
    infoslist: [],
    store_type: '',
    date: '',
    sum: '',
    num: '',
    totalPrice: '',
    totalNum: '',
    totalAllGoods: '',
    totalShareDis: '',
    totalShareMs: '',
    totalShareCoupon: '',
    totalMember: '',
    is_guide: '',
    isData: false,
    ipX: false,
  },

  onLoad(options) {
    wx.showLoading({
      title: '正在加载',
    });
    let corpid = wx.getStorageSync('corpid');

    this.setData({
      ipX: wx.systemInfo && wx.systemInfo.model.search('iPhone X') !== -1,
      is_guide: app.ws_micro_shop_globalData.role,
      isSaas: app.ws_micro_shop_globalData.isSaas,
      corpid
    });
    this.getInfoList();
    let {
      store_type = '',
      date = '',
      sum = '',
      num = '',
      totalPrice = '',
      totalNum = '',
      totalAllGoods = '',
      totalShareDis = '',
      totalShareMs = '',
      totalShareCoupon = '',
      totalMember = '',
    } = options;
    wx.setNavigationBarTitle({
      title: '分配清单',
    });
    this.setData({
      store_type,
      date,
      sum,
      num,
      totalPrice,
      totalNum,
      totalAllGoods,
      totalShareDis,
      totalShareMs,
      totalShareCoupon,
      totalMember,
    });
  },

  getInfoList() {
    let { is_guide } = this.data;
    let infos;
    if (Number(is_guide) === -1) {
      infos = JSON.parse(app.infomation.info)[1].list || [];
    } else if (Number(is_guide) === 0) {
      infos = JSON.parse(app.infomation.info) || [];
    }
    this.setData({
      infoslist: infos,
      isData: true,
    });
    wx.hideLoading();
  },

  submit: throttle(function() {
    let {
      is_guide = '',
      store_type = '',
      date = '',
      sum = '',
      num = '',
      totalPrice = '',
      totalNum = '',
      totalAllGoods = '',
      totalShareDis = '',
      totalShareMs = '',
      totalShareCoupon = '',
      totalMember = '',
      infoslist = '',
    } = this.data;
    let data = app.infomation || '';
    let url =
      Number(is_guide) === -1
        ? api.staff.performance_edit
        : Number(is_guide) === 0
        ? Number(store_type) === 0
          ? api.staff.target_submit
          : api.staff.task_submit
        : '';
    app.ws_micro_shoprequest({
      url: url,
      data: data,
      method: 'post',
      success: function (res) {
        wx.hideLoading();
        if (res.code === 0) {
          let url = '/ws_micro_shop_guide/pages/achieve-succ/index';
          let data = {
            store_type,
            date,
            sum,
            num,
            totalPrice,
            totalNum,
            totalAllGoods,
            totalShareDis,
            totalShareMs,
            totalShareCoupon,
            totalMember,
          };
          app.util.navigateTo(url, data, true);
        }
      },
    });
  }),
});
